package com.invoicing.controller;

import com.invoicing.MainController;
import com.invoicing.Service.*;
import com.invoicing.entity.Commodity;
import com.invoicing.entity.CommodityData;
import com.invoicing.view.MenuView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import org.springframework.beans.factory.annotation.Autowired;


import java.text.SimpleDateFormat;
import java.time.LocalDate;


import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

@FXMLController
public class ProfitController implements Initializable {


    List<Commodity> commodityList = new ArrayList<Commodity>();//所有商品
    private ObservableList<CommodityData> data = FXCollections.observableArrayList();
    float vendPrice = 0;
    int vendCount = 0;
    float sellPrice = 0;
    int sellCount = 0;
    float totalProfit = 0;
    float vendTotal = 0;
    float sellTotal = 0;
    Date startDate = new Date();
    Date endDate = new Date();
    SimpleDateFormat  dateformat = new SimpleDateFormat("yyyy-MM-dd");



    @Autowired
    private CommodityService commodityService;
    @Autowired
    private OrderitemService orderitemService;
    @Autowired
    private VenditemService venditemService;
    @Autowired
    private VendService vendService;
    @Autowired
    private OrderService orderService;




    @FXML
    private TableView table;
    @FXML
    private TableColumn id;
    @FXML
    private TableColumn name;
    @FXML
    private TableColumn vend_price;
    @FXML
    private TableColumn vend_count;
    @FXML
    private TableColumn sell_price;
    @FXML
    private TableColumn sell_count;
    @FXML
    private TableColumn profit;
    @FXML
    private Label total_profit;
    @FXML
    private DatePicker startTime;
    @FXML
    private DatePicker endTime;


    @FXML
    private void showAll(){

        commodityList = commodityService.listAll();
        data.clear();
        sellTotal = 0;
        vendTotal = 0;
        int i=0;
        try{
            startDate = dateformat.parse(startTime.getValue().toString());
            endDate = dateformat.parse(endTime.getValue().toString());
        }catch (Exception e){

        }
        if(startDate.getTime()>endDate.getTime()){
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setContentText("开始时间不得大于终止时间");
            alert.show();
        }
        for(i=0;i<commodityList.size();i++){
            Commodity commodity = commodityList.get(i);
            vendPrice = 0;
            vendCount = 0;
            sellPrice = 0;
            sellCount =0;
            try{
                vendPrice = venditemService.allVendCost(commodity.getId(),startDate,endDate);
                vendCount = venditemService.allVendCount(commodity.getId());
                sellPrice = orderitemService.getAllOrderCost(commodity.getId());
                sellCount = orderitemService.getAllOrderCount(commodity.getId());
            }
            catch (Exception e){

            }


            CommodityData commodityData = new CommodityData(commodity,i,"**",vendPrice,vendCount,sellPrice,sellCount);

            vendTotal = vendTotal+vendPrice;
            sellTotal = sellTotal+sellPrice;

        }
        table.setItems(data);


        if(vendTotal>sellTotal){
            totalProfit = vendTotal - sellTotal;
            total_profit.setText("-"+totalProfit);
        }
        else {
            totalProfit = sellTotal - vendTotal;
            total_profit.setText(totalProfit+"");
        }




    }
    @FXML
    private void setBack(){MainController.showView(MenuView.class);
    }

    public void initialize(URL location, ResourceBundle resources) {

        commodityList = commodityService.listAll();
        sell_count.setCellValueFactory(new PropertyValueFactory<CommodityData, Integer>("sell_count"));
        sell_price.setCellValueFactory(new PropertyValueFactory<CommodityData, Float>("sell_price"));
        vend_count.setCellValueFactory(new PropertyValueFactory<CommodityData, Integer>("vend_count"));
        vend_price.setCellValueFactory(new PropertyValueFactory<CommodityData, Float>("vend_price"));
        id.setCellValueFactory(new PropertyValueFactory<CommodityData, Integer>("id"));
        name.setCellValueFactory(new PropertyValueFactory<CommodityData, String>("name"));
        profit.setCellValueFactory(new PropertyValueFactory<CommodityData, Float>("profit"));
        startTime.setValue(LocalDate.of(2000,1,1));
        endTime.setValue(LocalDate.of(2000,1,1));
        endTime.setValue(LocalDate.now());
        showAll();

    }
}
